﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class Frmcapnhatphieukham : PRE.catalog.FrmBase
    {
        DAL.PhieukhamDAL _dal = new DAL.PhieukhamDAL();
        public Frmcapnhatphieukham()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            base.PerformAdd();
        }
        
        /// <summary>
        /// Perform when click cancel button
        /// </summary>
protected override void PerformCancel()
        {
            ChangeStatus_PhieukhamBacSi();
            base.PerformCancel();
        }
        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            
            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus_PhieukhamBacSi(false);
            cbotrangthaikham.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            string stt = grvMain.GetFocusedRowCellValue("STTPHIEU") + "";
            DateTime ngay = Convert.ToDateTime(grvMain.GetFocusedRowCellValue("NGAYKHAM") + "");
            DataTable dtb_report = _dal.Select_Report(stt, ngay);

            var frm = new Report.Report_Base();
            var xr = new Report.XRPhieukham();

            xr.DataSource = dtb_report;
            frm.SetReport(xr);

            frm.Show();
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {


            if (IsAdd)
            {
                var o = new DAL.Entities.phieukham()
                {
                    sttphieu = txtsttphieu.Text.Trim(),
                    ngaykham = Convert.ToDateTime(dtengaykham.EditValue),
                    ketluanbenh = txtketluanbenh.Text.Trim(),
                    matrangthai = (cbotrangthaikham.EditValue + "").Trim(),
//                    manv = (cbomanv.EditValue + "").Trim(),
                    trangthaithanhtoan = false

                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieukhamBacSi(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieukhamBacSi(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                //var _mabn = grvMain.GetFocusedRowCellValue("MaBN") + "";
                var o = new DAL.Entities.phieukham()
                {

                    sttphieu = txtsttphieu.Text.Trim(),
                    ngaykham = Convert.ToDateTime(dtengaykham.EditValue),
                    ketluanbenh = txtketluanbenh.Text.Trim(),
                    matrangthai = (cbotrangthaikham.EditValue + "").Trim(),
  //                  manv = (cbomanv.EditValue + "").Trim(),
                    trangthaithanhtoan = false

                };

                var oki = _dal.Update_bacsi(o);
                if (oki)
                {
                    MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus_PhieukhamBacSi();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus_PhieukhamBacSi(false);
                    ReadOnlyControl(false);
                }
            }

            ChangeStatus_PhieukhamBacSi();
            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            
            
            txtketluanbenh.Text = null;
            
            
    //        cbomanv.ItemIndex= -1;
            
            cbotrangthaikham.ItemIndex = -1;
            
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtsttphieu.DataBindings.Clear();
            
            dtengaykham.DataBindings.Clear();
            
            txtketluanbenh.DataBindings.Clear();

      //      cbomanv.DataBindings.Clear();
            
            cbotrangthaikham.DataBindings.Clear();
            

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtsttphieu.DataBindings.Add("EditValue", _dtb, ".STTPHIEU");
            
            dtengaykham.DataBindings.Add("EditValue", _dtb, ".NGAYKHAM");
            
            txtketluanbenh.DataBindings.Add("EditValue", _dtb, ".KETLUANBENH");

        //    cbomanv.DataBindings.Add("EditValue", _dtb, ".MA_NV");
            
            cbotrangthaikham.DataBindings.Add("EditValue", _dtb, ".MATRANGTHAI");
            


            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txtsttphieu.Properties.ReadOnly = true;
            dtengaykham.Properties.ReadOnly = true;
            
            
            txtketluanbenh.Properties.ReadOnly = isReadOnly;

          //  cbomanv.Properties.ReadOnly = isReadOnly;
            
            cbotrangthaikham.Properties.ReadOnly = isReadOnly;
            

            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion


        private void Frmcapnhatphieukham_Load(object sender, EventArgs e)
        {
            cbotrangthaikham.Properties.DataSource = _dal.Select_trangthaikham();
           // cbomanv.Properties.DataSource = _dal.Select_nhanvien();
            ChangeStatus_PhieukhamBacSi();
            PerformRefresh();
        }

        private void dockPanel1_Click(object sender, EventArgs e)
        {

        }
        public static string _mabn;
        private void btxemlichsubenhnhan_Click(object sender, EventArgs e)
        {
            _mabn = grvMain.GetFocusedRowCellValue("MA_BN") + "";

            var frm = new catalog.FrmXemlichsubenh();
            frm._mabn = _mabn;
            frm.Show();
        }
        public static string _sttphieu;
        public static string _ngaykham;
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            _sttphieu = txtsttphieu.Text;
            _ngaykham = dtengaykham.Text;
            var frm = new catalog.FrmPhieusudungdichvu();
            frm._sttphieu1 = _sttphieu;
            frm._ngaykham1 = _ngaykham;
            frm.ShowDialog();
        }
    }
}
